﻿CREATE TABLE [dbo].[Address] (
    [ID] [int] NOT NULL IDENTITY,
    [Address1] [nvarchar](max),
    [Address2] [nvarchar](max),
    [City] [nvarchar](max),
    [State] [nvarchar](max),
    [Zip] [nvarchar](max),
    CONSTRAINT [PK_dbo.Address] PRIMARY KEY ([ID])
)
CREATE TABLE [dbo].[Position] (
    [ID] [int] NOT NULL IDENTITY,
    [Latitude] [float] NOT NULL,
    [Longitude] [float] NOT NULL,
    [InsertDate] [datetime] NOT NULL,
    [User_ID] [int] NOT NULL,
    [Address_ID] [int] NOT NULL,
    CONSTRAINT [PK_dbo.Position] PRIMARY KEY ([ID])
)
CREATE INDEX [IX_User_ID] ON [dbo].[Position]([User_ID])
CREATE INDEX [IX_Address_ID] ON [dbo].[Position]([Address_ID])
CREATE TABLE [dbo].[User] (
    [ID] [int] NOT NULL IDENTITY,
    [Username] [nvarchar](max),
    [Email] [nvarchar](max),
    [Password] [nvarchar](max),
    [Salt] [nvarchar](max),
    [InsertDate] [datetime] NOT NULL,
    [ModifiedDate] [datetime] NOT NULL,
    CONSTRAINT [PK_dbo.User] PRIMARY KEY ([ID])
)
ALTER TABLE [dbo].[Position] ADD CONSTRAINT [FK_dbo.Position_dbo.User_User_ID] FOREIGN KEY ([User_ID]) REFERENCES [dbo].[User] ([ID]) ON DELETE CASCADE
ALTER TABLE [dbo].[Position] ADD CONSTRAINT [FK_dbo.Position_dbo.Address_Address_ID] FOREIGN KEY ([Address_ID]) REFERENCES [dbo].[Address] ([ID]) ON DELETE CASCADE
CREATE TABLE [dbo].[__MigrationHistory] (
    [MigrationId] [nvarchar](255) NOT NULL,
    [Model] [varbinary](max) NOT NULL,
    [ProductVersion] [nvarchar](32) NOT NULL,
    CONSTRAINT [PK_dbo.__MigrationHistory] PRIMARY KEY ([MigrationId])
)
BEGIN TRY
    EXEC sp_MS_marksystemobject 'dbo.__MigrationHistory'
END TRY
BEGIN CATCH
END CATCH